<?php
include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("db.inc.php");							//--> db global var
include_once("Smarty.class.php");					//--> out template
include_once("Operation.php");						//--> Operation
include_once("PromotionImpl.php");						//--> Special
include_once("Product.php");						//--> Product
require_once("controlHeader.php");					//--> system control header

$objOperate 	= new Operation($objSession->getLanguage());		//--> Operation
$objPromoImpl	= new PromotionImpl(DB_TAG_SYSTEM, $uid);					//--> Special
$oProduct 		= new Product(DB_TAG_SYSTEM, $uid);					//--> Product

$error_message 	= '';

$productID		= '';
$descript		= '';

$arrOperate = $objOperate->arrGetFromGroupIDAndMenuID($gid, $menuid);
if(!empty($id)){
	$arrData 		= $objPromoImpl->getById($arrOperate, $id);
	
	$productID		= $arrData['productID'];
	$descript 		= $arrData['descript'];
}

if(isset($_POST['Submit'])){
	$id 		= $_POST['id'];
	$productID	= $_POST['productID'];
	$descript 	= $_POST['descript'];
 	
  	
	if(empty($productID)){
		$error_message = 'The product ID should\'t be empty.';
	}else if(empty($descript)){
		$error_message = 'The promotion description should\'t be empty.';
	}
	
	if(empty($error_message)){
		$isReturnOrg = $objPromoImpl->edit($arrOperate, $id, $productID, $descript);
		if($isReturnOrg)
			$error_message = 'edit successfully.';
		else{
			$error_message = 'edit failure1.';
		}
	}
}

/*--------- get product list ---------------------*/
$arrProducts = array();
$where	= ' AND p.status in ("normal", "hot")';
$order	= 'ORDER BY pd.name';

$arrDataList = $oProduct->lists($arrOperate, $LANGEUAGE_ID, $where, $order, 0, 10000, '');
foreach($arrDataList as $arrRow){
	$arrProducts[$arrRow['id']] = $arrRow['name'] . " ($CURRENCY" . ($arrRow['price'] / 100) . ')';
}
 
/*----- out html -----*/
$smarty = new Smarty();		//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = COMPILE_SYS_DIR;
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);
$smarty->assign('id', $id);

$smarty->assign('error_message', $error_message);
$smarty->assign('backurl',$backurl);

$smarty->assign('arrProducts', $arrProducts);
$smarty->assign('descript', $descript);
$smarty->assign('productID', $productID);

$smarty->display('listPromotion_Edit.htm');
?>
